var express = require('express');
const { sellersModel } = require('../model/sellersModel');
var router = express.Router();

const { userModel } = require("../model/userModel")

/* GET users listing.通过populate在传输时，获取所属销售员的信息 */
router.get('/userList', async function (req, res) {
  console.log("获取用户信息列表");
  const rows = await userModel.find().populate("sellersId")
  res.send({
    code: 1,
    message: "获取用户信息列表",
    rows
  })
});


//查看单个顾客对应的信息
router.get('/getOneUser/:_id', async function (req, res) {
  console.log("查看单个顾客对应的信息");
  const rows = await userModel.findById(req.params._id).populate("sellersId")
  // .populate("sellersId")
  res.send({
    code: 1,
    message: "获取用户信息",
    rows
  })

});

//查看我负责的所有客户的信息
router.get('/myUserList/:sellersAccount', async function (req, res) {
  const rows = await userModel.find().populate("sellersId")
  // //AJAX发送时，应用myUserList/${account}传输，接收时用req.params接收
  let newdata = rows.filter(item => {
    return item.sellersId.sellersAccount == req.params.sellersAccount
  })
  res.send({
    code: 1,
    message: "获取用户信息列表",
    newdata
  })
});


//修改客户信息

router.put('/modifyOneUser', async function (req, res) {
  //这是全部用户信息把我发送的销售员名字，替换为_id
  const alluser = await sellersModel.find()
  console.log(alluser);
  //这是遍历后匹配的结果
  let result = alluser.filter(item => {
    return item.sellersName == req.body.sellersId
  })
  //这是替换步
  console.log(result);
  req.body.sellersId = result[0]._id
  console.log(req.body);
  let newID = req.body._id
  delete req.body._id

  const { modifiedCount } = await userModel.updateOne({ _id: newID }, req.body).populate("sellersId")

  if (modifiedCount) {
    res.send({
      code: 1,
      message: "修改成功",
      rows: modifiedCount
    })
  }
});

//增加一个客户
router.post('/addUser', async function (req, res) {

  const allsellers = await sellersModel.find()

  //这是遍历后匹配的结果
  let result = allsellers.filter(item => {
    return item.sellersName == req.body.sellersId
  })
  //这是替换步
  req.body.sellersId = result[0]._id

  const rows = await userModel.create(req.body)

  //AJAX发送时，应用delOneUser/${account}传输，接收时用req.params接收

  res.send({
    code: 1,
    message: "新增用户成功",
    rows
  })
});

//删除单个客户信息
router.put('/delOneUser/:_id', async function (req, res) {
  const result = await userModel.findById(req.params._id).populate("sellersId")

  result.delete = true;
  let newId = result._id
  delete result._id
  const arr = await userModel.updateOne({ _id: newId }, result)
  res.send({
    code: 1,
    message: "删除成功",
    rows: arr
  })
});

//删除多个客户信息
router.delete('/delManyUser', async function (req, res) {
  const rows = await userModel.deleteMany({ userAccount: { $in: req.body } }).populate("sellersId")
  //AJAX发送时，应用delOneUser/${account}传输，接收时用req.params接收
  res.send({
    code: 1,
    message: "删除用户成功",
    rows
  })
});

//模糊查询（输入account里的关键字）
router.get('/getThatUser/:userName', async function (req, res) {
  const rows = await userModel.find({ userName: { $regex: req.params.userName } }).populate("sellersId")
  //AJAX发送时，应用delOneUser/${account}传输，接收时用req.params接收
  res.send({
    code: 1,
    message: "查询用户成功",
    rows
  })
});



module.exports = router;
